package cn.gz.demo.ui;


import android.os.Bundle;
import android.util.Pair;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.gz.demo.R;
import cn.gz.demo.databinding.TabLayoutViewPagerBinding;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.ui.tab_fragment.FoundFragment;
import cn.gz.demo.ui.tab_fragment.MeFragment;
import cn.gz.demo.ui.tab_fragment.MessageFrament;

/**
 * Created by Lyz on 2017/3/8.
 */
public class TabLayoutViewPagerActivity extends BaseActivity {
    private TabLayoutViewPagerBinding binding;
    private List<Pair<String, Fragment>> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_tablayout_viewpager);

        initView();
    }

    private void initView() {

        items = new ArrayList<>();
        items.add(new Pair<String, Fragment>("OkGo", new MessageFrament()));
        items.add(new Pair<String, Fragment>("打赏", new FoundFragment()));
        items.add(new Pair<String, Fragment>("OkRx2", new MeFragment()));
        items.add(new Pair<String, Fragment>("OkRx", new MessageFrament()));
        items.add(new Pair<String, Fragment>("OkDownload", new FoundFragment()));
        items.add(new Pair<String, Fragment>("OkUpload", new MeFragment()));

        binding.tabViewpager.setAdapter(new MainAdapter(getSupportFragmentManager()));
        binding.tabTablayout.setupWithViewPager( binding.tabViewpager);
    }

    private class MainAdapter extends FragmentPagerAdapter {

        MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return items.get(position).second;
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return items.get(position).first;
        }
    }
}
